/**
 * 管理Token
 * @author 木人心
 * @version 1.0.0
 */
import request from '@utils/request'
import { axiosAuthConfig } from '@utils/request/config'
import { authorizationConfig } from '@utils/authorization/config'
import { storage } from '@utils/storage'
import { useDeviceRoleStore } from '@/dataset/device-role/store'
import { useAppStore } from '@/pinia'
import { UserInfo } from '@typings/user'

/**
 * 获取令牌
 */
export function getToken () {
  return storage.localStorage.getItem(authorizationConfig.tokenKey)
}

/**
 * 设置令牌
 * @param value
 */
export function setToken (value: string) {
  request.defaults.headers.common[axiosAuthConfig.authorizationKey] = value
  storage.localStorage.setItem(authorizationConfig.tokenKey, value)
}

/**
 * 清除令牌
 */
export function removeToken () {
  storage.localStorage.clear()
  storage.sessionStorage.clear()
  useAppStore().setUserInfo({} as UserInfo)
  useDeviceRoleStore().deviceRole.clear()
  delete request.defaults.headers.common[axiosAuthConfig.authorizationKey]
}
